package com.meida.module.system.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.meida.common.constants.CommonConstants;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.utils.ApiAssert;
import com.meida.module.system.client.entity.SysSlider;
import com.meida.module.system.provider.mapper.SysSliderMapper;
import com.meida.module.system.provider.service.SysSliderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 轮播图 实现类
 *
 * @author flyme
 * @date 2019-07-05
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysSliderServiceImpl extends BaseServiceImpl<SysSliderMapper, SysSlider> implements SysSliderService {

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<EntityMap> listBySliderType(int sliderType, int count) {
        CriteriaQuery cq = new CriteriaQuery(SysSlider.class);
        cq.select("sliderId", "sliderTitle", "sliderImg", "jumpType", "content");
        cq.eq("sliderType", sliderType);
        cq.eq("sendStatus", CommonConstants.ENABLED);
        cq.orderByDesc("createTime");
        cq.last("limit " + count);
        return selectEntityMap(cq);
    }

    @Override
    public ResultBody setSendStatus(Long[] ids, Integer sendStatus) {
        ApiAssert.isNotEmpty("主键不能为空", ids);
        UpdateWrapper product = new UpdateWrapper();
        product.set("sendStatus", sendStatus);
        product.in("sliderId", ids);
        boolean result = update(product);
        if (result) {
            return ResultBody.ok("操作成功", sendStatus);
        } else {
            return ResultBody.failed("操作失败");
        }
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<SysSlider> cq, SysSlider sysSlider, EntityMap requestMap) {
        cq.likeByField(SysSlider.class, "sliderTitle");
        return ResultBody.ok();
    }
}
